package com.luyuan.crm.question.web;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.faces.model.DataModel;

import org.hibernate.criterion.Order;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.luyuan.crm.question.model.Question;
import com.luyuan.crm.question.model.QuestionExtend;
import com.luyuan.crm.question.model.QuestionField;
import com.luyuan.crm.question.service.QuestionExtendService;
import com.luyuan.crm.question.service.QuestionService;
import com.luyuan.crm.question.service.QuestionValueService;
import com.ruijie.foundation.query.Page;
import com.ruijie.foundation.query.criteria.CriteriaTemplate;
import com.ruijie.foundation.query.criteria.RestrictionTemplate;
import com.ruijie.web.jsf.action.BaseAction;
import com.ruijie.web.jsf.context.TableContext;
import com.ruijie.web.jsf.model.PageDataModel;
import com.ruijie.web.jsf.model.TableOrder;
import com.ruijie.web.jsf.model.TablePage;

@Component("questionExtendAction")
@Scope("view")
public class QuestionExtendAction extends BaseAction implements InitializingBean {

    private Question question;

    private QuestionExtend questionExtend;

    @Resource(name = "questionService")
    private QuestionService questionService;

    private QuestionField questionFieldType;

    @Resource(name = "questionExtendService")
    private QuestionExtendService questionExtendService;

    @Resource(name = "questionValueService")
    private QuestionValueService questionValueService;

    public void addQuestionExtend() {
        questionExtend.setQuestion(question);
        questionExtendService.saveQuestionExtend(questionExtend);
    }

    public DataModel getTableModel() {
        TablePage tablePage = TableContext.getTablePage();
        List<TableOrder> tableOrders = TableContext.getTableOrders();
        List<Order> orders = new ArrayList<Order>();
        for (TableOrder tableOrder : tableOrders) {
            Order order = tableOrder.toOrder();
            if (order != null) {
                orders.add(order);
            }
        }

        CriteriaTemplate c = CriteriaTemplate.forClass(QuestionExtend.class);
        RestrictionTemplate r = new RestrictionTemplate(false);
        c.add(r.eq("question", question));

        Page page = questionExtendService.pagedQuery(c, tablePage.getPage(), tablePage.getPageSize(), orders);
        return new PageDataModel(page);
    }

    public void delQuestion(int index) {
        questionService.remove(index);
        questionValueService.removeByQuestionId(index);
    }

    public void delQuestionExtend(int id) {
        questionExtendService.removeById(id);
    }

    public boolean hasOptions(int id) {
        QuestionExtend extend = questionExtendService.getById(id);

        if (extend.getItemType() == QuestionField.RADIO) {
            return true;
        }
        if (extend.getItemType() == QuestionField.TEXT) {
            return false;
        }
        if (extend.getItemType() == QuestionField.TEXTAREA) {
            return false;
        }

        return false;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        String id = getRequestParameter("uuid");
        if (id != null) {
            question = questionService.safeGet(Long.parseLong(id));
        }
    }

    public QuestionField getQuestionFieldType() {
        return questionFieldType;
    }

    public void setQuestionFieldType(QuestionField questionFieldType) {
        this.questionFieldType = questionFieldType;
    }

    public Question getQuestion() {
        return question;
    }

    public void setQuestion(Question question) {
        this.question = question;
    }

    public QuestionExtend getQuestionExtend() {
        return questionExtend;
    }

    public void setQuestionExtend(QuestionExtend questionExtend) {
        this.questionExtend = questionExtend;
    }

}
